﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace CleanToolkit.Formating
{
    public class FieldsResNameRemover
    {
        public string FieldsFilePath { get; set; }
        public string NamesFilePath { get; set; }

        public XDocument GetFieldWithoutResNames()
        {
            // Chargement des fichiers
            XDocument xFields = XDocument.Load(FieldsFilePath);
            XDocument xNames = XDocument.Load(NamesFilePath);

            // Chargement des champs
            IEnumerable<XElement> xResFields = xFields
                .Elements(CleanConstants.CST_SpNamespace + "Elements")
                .Elements(CleanConstants.CST_SpNamespace + "Field");

            IEnumerable<XElement> xResFieldRefs = xFields
                .Descendants(CleanConstants.CST_SpNamespace + "FieldRef");

            IEnumerable<XElement> xNameFields = xNames
                .Elements("root")
                .Elements("data");

            // Remplacement
            //***************************************************

            // Dans les FieldRef
            foreach (XElement xResField in xResFieldRefs)
            {
                // Remplacement des ressources pour [Name] et [StaticName]
                if (xResField.Attribute("Name").Value.StartsWith("$"))
                {
                    // Nom de la ressource
                    string resName = xResField.Attribute("Name").Value;
                    resName = resName.Substring(1 + resName.IndexOf(","));
                    resName = resName.Replace(";", "");

                    // Recherche du noeud de ressource correspondant
                    XElement xNameField = xNameFields.Where(xe => xe.Attribute("name").Value == resName).FirstOrDefault();
                    if (xNameField != null)
                    {
                        // Remplacement du lien vers la ressource par la valeur
                        xResField.SetAttributeValue("Name", xNameField.Elements("value").First().Value);
                    }
                }

            }

            // Dans les Field
            foreach (XElement xResField in xResFields)
            {
                // Remplacement des ressources pour [Name] et [StaticName]
                if (xResField.Attribute("Name").Value.StartsWith("$"))
                {
                    // Nom de la ressource
                    string resName = xResField.Attribute("Name").Value;
                    resName = resName.Substring(1 + resName.IndexOf(","));
                    resName = resName.Replace(";", "");

                    // Recherche du noeud de ressource correspondant
                    XElement xNameField = xNameFields.Where(xe => xe.Attribute("name").Value == resName).FirstOrDefault();
                    if (xNameField != null)
                    {
                        // Remplacement du lien vers la ressource par la valeur
                        xResField.SetAttributeValue("Name", xNameField.Elements("value").First().Value);
                        xResField.SetAttributeValue("StaticName", xNameField.Elements("value").First().Value);
                    }
                }

                // Suppression de l'attribut "EnforceUniqueValues"
                XAttribute xAttr = xResField.Attribute("EnforceUniqueValues");
                if (xAttr != null)
                {
                    xAttr.Remove();
                }

                // Tri des attributs
                IEnumerable<XAttribute> sortedAttr = xResField.Attributes().OrderBy(xa =>
                {
                    int sortIndex = 10;

                    if (xa.Name == "ID")
                    {
                        sortIndex = 0;
                    }
                    else if (xa.Name == "Type")
                    {
                        sortIndex = 1;
                    }
                    else if (xa.Name == "Name")
                    {
                        sortIndex = 2;
                    }
                    else if (xa.Name == "StaticName")
                    {
                        sortIndex = 3;
                    }
                    else if (xa.Name == "DisplayName")
                    {
                        sortIndex = 4;
                    }
                    else if (xa.Name == "Description")
                    {
                        sortIndex = 5;
                    }
                    else if (xa.Name == "Group")
                    {
                        sortIndex = 6;
                    }

                    return sortIndex;
                });

                xResField.ReplaceAttributes(sortedAttr);

                //Console.WriteLine(sortedAttr);

            }

            return xFields;
        }
    }
}
